package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.ParseState;
import com.kdtech.crawler.CrawlHTML;

import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

public class BlancaBbsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		String[] regex = {
				"http://www.blanca.com.cn/blanca-[0-9]*-1-1.html",
				"http://www.blanca.com.cn/thread-[0-9]*-1-1.html",
				};
		return RegexUtils.matchAny(url, regex);
	}

	public NewsMeta parserHtml(UrlMeta urlMeta) {

		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();

		NewsMeta news = new NewsMeta();
		String title = null;
		String content = null;
		Long date = null;
		String dateStr = null;
		String commnetNum = null;
		String clickNum = null;
		String author = null;
		Document doc = Jsoup.parse(htmltxt);
		Element first = doc.select("div.infotop div.infotop1 div.infotop1_div1 dd.dd1 a strong").first();
		if(first!=null){
			author = first.text();
		}
		if(doc.select("div#messagetext.alert_error").size() > 0){
			news.setUpdateUrl(ParseState.ERR404.toString());
			return news;
		}
		title = doc.select("div.page_title").text();
		if(StringUtils.isBlank(title)){
			title = doc.select("#thread_subject").text();
			dateStr = doc.select("div.authi em span").attr("title");
			date = DateUtils.matchDate(dateStr);
			if(date==null){
				dateStr = doc.select("div.authi").text();
				date = DateUtils.matchDate(dateStr);
			}
			Elements select2 = doc.select("td.t_f");
			if(select2.size()>0){
				Element element = select2.get(0);
				element.select(".attach_nopermission").remove();
				content = HtmlCleaner.getContentHtml(url, element);
			}
			Elements select = doc.select("td.pls div.hm span");
			if(select.size()==5){
				clickNum= select.get(1).text();
				commnetNum= select.get(4).text();
			}
		}else{
			dateStr = doc.select("div.time").text();
			date = DateUtils.matchDate(dateStr);
			Elements select2 = doc.select("div.t_fsz");
			if(select2.size()>0){
				Element element = select2.get(0);
				element.select(".attach_nopermission,div.locked,dl.rate").remove();
				content = HtmlCleaner.getContentHtml(url,  element);
			}
			Elements select = doc.select(" div.infotop div.infotop1 div.infotop1_div3 dd");
			if(select.size()==2){
				clickNum= select.get(0).ownText().replace("：", "");
				commnetNum= select.get(1).ownText().replace("：", "");
			}
		}
		if(doc.select("div#messagetext.alert_error,div#messagelogin").size()>0){
			news.setUpdateUrl(ParseState.ERR404.toString());
			return news;
		}
		if(doc.select("h3:contains(本版块需要密码，您必须在下面输入正确的密码才能浏览这个版块)").size()>0){
			news.setUpdateUrl(ParseState.FORBID.toString());
			return news;
		}
		/**
		 * 初始化、截取 、设置作者信息*******************************************
		 *
		 * */
		if(doc.select(".authi").size() > 0)
		author = doc.select(".authi").get(0).text();

		/**
		 *                        	 **************************end *************************
		 *
		 **/
			news.setUrl(url);
			news.setType(4);
			news.setAuthor(author);
			news.setTitle(StringUtils.trimSpace(title));
			news.setCommentNum(NumberUtils.parseInt(commnetNum));
			news.setClickNum(NumberUtils.parseInt(clickNum));
			news.setContent(StringUtils.trimSpace(content));
			news.setDate(date);
			news.setUpdateUrl(url);
			return news;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		String url = meta.getUpdateUrl();
		if (StringUtils.isNotBlank(url)){
			UrlMeta urlMeta = CrawlHTML.responseToURL(url);
			return parserHtml(urlMeta);
		}
		return null;
	}

	

	public static void main(String[] args) {
		BlancaBbsAnalyse a = new BlancaBbsAnalyse();
		String url = "http://www.blanca.com.cn/blanca-790627-1-1.html";
		url = UrlArgumentTop.FromatUrl(url);
		System.out.println(url);
		System.out.println(DoMainUtils.GetDomainName(url));
		if(a.isDetailPage(url)){
			UrlMeta meta = CrawlHTML.responseToURL(url);
			NewsMeta parserHtml = a.parserHtml(meta);
			System.out.println(parserHtml);
		}else{
			System.out.println("正则不匹配");
		}
	}

}
